package com.zhuzg.oauth2.service;


import com.zhuzg.oauth2.domain.Client;
import com.zhuzg.oauth2.mapper.ClientDetailsMapper;
import com.zhuzg.oauth2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 管理的客户端
 */
@Service("myClientservice")
public class ClientDetailsServiceImpl implements ClientDetailsService {

   @Autowired
   ClientDetailsMapper clientDetailsMapper;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        Client client = clientDetailsMapper.findByid(clientId);
        if(client==null){
            throw  new RuntimeException("客户id 不存在");
        }
        ClientDetails clientDetails = this.cli2Details(client);
        return clientDetails;
    }

    private ClientDetails cli2Details(Client client){
        //  client====>ClientDetails  类型的转换
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId(client.getClientId());
        clientDetails.setClientSecret(client.getClientSecret());

        // clientDetails.setAuthorities();
        String[] grantTypes = client.getAuthorizedGrantTypes().split(",");// 根据都好分割程数组
        List<String> grantTypesList = Arrays.asList(grantTypes);
        clientDetails.setAuthorizedGrantTypes(grantTypesList);

        List<String> scopes = Arrays.asList(client.getScope().split(","));// 字符串转换成list集合
        Set<String> scopeSet = scopes.stream().collect(Collectors.toSet());// List  -> stream   -->  set 集合
        clientDetails.setScope(scopeSet);

        // 设置accessToken和refreshToken的时效，如果不设置则使tokenServices的配置的
        clientDetails.setAccessTokenValiditySeconds((int) TimeUnit.HOURS.toSeconds(client.getAccessTokenValidity()));
        clientDetails.setRefreshTokenValiditySeconds((int)TimeUnit.DAYS.toSeconds(client.getRefreshTokenValidity()));
        clientDetails.setAuthorities(new ArrayList<>());


        clientDetails.setResourceIds(this.strToList(client.getResourceIds()));
        clientDetails.setRegisteredRedirectUri(this.strToList(client.getWebServerRedirectUri()).stream().collect(Collectors.toSet()));

        return  clientDetails;

    }

    /**
     *  字符串 根据都好分割 转换成 数组
     * @param str
     * @return
     */
    private  List<String> strToList(String  str){
        String[] arr =str.split(",");// 根据都好分割程数组
        List<String> strList = Arrays.asList(arr);
        return strList;
    }


}
